package com.integrationpath.mengine.webapp.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.gps.CompassGps;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.util.SimpleStatisticListenerImpl;
import com.integrationpath.mengine.webapp.action.BaseAction;

/**
 * This class is used to reload the drop-downs initialized in the
 * StartupListener.
 * 
 * @author <a href="mailto:colaru@gmail.com">Cristian Olaru</a>
 */
public class CompassAction extends BaseAction {
	private static final long serialVersionUID = 295460450224891051L;
	private static transient final Log log = LogFactory.getLog(SimpleStatisticListenerImpl.class);
	private String search;
	List articles = new ArrayList();
	

	public List getArticles() {
		return articles;
	}

	public void setArticles(List articles) {
		this.articles = articles;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String reindex() throws IOException {
		log.info("Enter reIndex method");
		CompassGps compassGps = (CompassGps) getSpringContext().getBean(
				"compassGps");
		boolean isRun = compassGps.isRunning();
		log.info("isRun : " + isRun);
		if (!isRun)
			compassGps.start();
		compassGps.index();
		return SUCCESS;
	}

	public String executeSearch() throws IOException {
		
		log.info("Enter search method");
		Compass compass = (Compass) getSpringContext().getBean("compass");
		CompassSession session = compass.openSession();
		CompassTransaction tx = null;
		try {
			tx = session.beginTransaction();

			CompassHits hits = session.find(search);
			if(hits != null)
				prepareResponse(hits);
			for (int i = 0; i < hits.length(); i++) {
				Object object = hits.data(i);
				Article article = null;
				if (object instanceof Article) {
					article = (Article) object;
					log.info("\n" + "Article ->>> [ Id ] "
							+ article.getId() + "\n" + " [ Title ] "
							+ article.getTitle() + " [ Content ] "
							+ article.getContent() + "\n");
				}
			}
			tx.commit();
		} catch (CompassException ce) {
			if (tx != null)
				tx.rollback();
		} finally {
			session.close();
		}
		return SUCCESS;
	}

	WebApplicationContext getSpringContext() {
		return WebApplicationContextUtils.getWebApplicationContext(getRequest()
				.getSession().getServletContext());
	}
	
    private void prepareResponse(CompassHits hits) {
    	for (int i = 0; i < hits.length(); i++) {	
    		Article article = (Article)hits.data(i); 
    		articles.add(article);
     	}
        }
}
